package com.qf.courtesycard.provide.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.courtesycard.provide.entity.Ticket;
import com.qf.courtesycard.provide.mapper.TicketMapper;
import com.qf.courtesycard.provide.service.ITicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lx
 * @since 2023-03-10
 */
@Service
public class TicketServiceImpl extends ServiceImpl<TicketMapper, Ticket> implements ITicketService {

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createTicket() throws JsonProcessingException {
        List<Ticket> tickets = new ArrayList<>();
        ObjectMapper objectMapper = new ObjectMapper();
        for (int i = 0; i < 100; i++) {
            Ticket tikcet = getTikcet();
            String json = objectMapper.writeValueAsString(tikcet);
            redisTemplate.opsForList().leftPush("ticketlist", json);
            tickets.add(tikcet);
        }
        //落库
        saveBatch(tickets);

    }

    /**
     * 根据id修改ticket状态是否抢券
     *
     * @param ticket
     * @return
     */
    @Override
    public String updateStatusById(Ticket ticket) {
        boolean flag = update(ticket, new QueryWrapper<Ticket>().eq("id", ticket.getId()));
        if (flag) {
            return "抢券成功，状态已更改";
        } else {
            return "抢券失败";
        }

    }

    /**
     * 发放优惠券
     *
     * @return
     */
    @Override
    public Ticket distributeTicket() throws Exception {
        String ticketToJson = (String) redisTemplate.opsForList().leftPop("ticketlist");
        ObjectMapper objectMapper = new ObjectMapper();
        Ticket ticket = objectMapper.readValue(ticketToJson, Ticket.class);
        if (ticket == null) {
            throw new Exception("当前无可用优惠券");
        }
        ticket.setStatus(1);
        return ticket;
    }

    /**
     * 生成优惠券
     *
     * @return
     */
    private Ticket getTikcet() {
        Ticket ticket = new Ticket();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //生成UUID
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        ticket.setId(uuid);
        //设置优惠券金额
        ticket.setPrice(100.00);
        //优惠券创建时间
        ticket.setCreatetime(new Date());
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 10);
        //设置10天有效期
        ticket.setExpirationdate(new Date(calendar.getTimeInMillis()));
        //设置使用范围
        ticket.setScope("满1000减100");

        ticket.setStatus(0);
        return ticket;
    }
}
